﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ABDLight
{
    public partial class frmFilter : Form
    {
        DataTable tblOper = new DataTable();
        DataTable tblFilter = new DataTable();
        DataTable tblFields = new DataTable();
        Dictionary<string, int> cols = new Dictionary<string, int>();

        public string fltStr = "";
        public frmFilter()
        {
            InitializeComponent();
            if (tblFilter.Columns.Count == 0)
            {
                tblFilter.Columns.Add(new DataColumn("c_Columns", typeof(System.String)));
                tblFilter.Columns.Add(new DataColumn("c_oper", typeof(System.String)));
                tblFilter.Columns.Add(new DataColumn("c_value", typeof(System.String)));
                tblFilter.Columns.Add(new DataColumn("c_type", typeof(System.Int32)));
            }
            grdFilter.AutoGenerateColumns = false;
            grdFilter.Columns["c_Columns"].DataPropertyName = "c_Columns";
            grdFilter.Columns["c_oper"].DataPropertyName = "c_oper";
            grdFilter.Columns["c_value"].DataPropertyName = "c_value";
            grdFilter.Columns["c_type"].DataPropertyName = "c_type";

            grdFilter.DataSource = tblFilter;

            tblFields.Columns.Add(new DataColumn("c_value", typeof(System.String)));
            tblFields.Columns.Add(new DataColumn("c_text", typeof(System.String)));

            tblOper.Columns.Add(new DataColumn("c_value"));
            tblOper.Columns.Add(new DataColumn("c_text"));

            DataRow eq = tblOper.NewRow();
            //eq["c_value"] = "eq";
            eq["c_value"] = "=";
            eq["c_text"] = "равно";
            tblOper.Rows.Add(eq);

            DataRow le = tblOper.NewRow();
            // le["c_value"] = "le";
            le["c_value"] = "<";
            le["c_text"] = "меньше";
            tblOper.Rows.Add(le);

            DataRow re = tblOper.NewRow();
            //re["c_value"] = "re";
            re["c_value"] = ">";
            re["c_text"] = "больше";
            tblOper.Rows.Add(re);

            DataRow ne = tblOper.NewRow();
            // ne["c_value"] = "ne";
            ne["c_value"] = "<>";
            ne["c_text"] = "не равно";
            tblOper.Rows.Add(ne);

            DataRow like = tblOper.NewRow();
            // like["c_value"] = "Like";
            like["c_value"] = "Like";
            like["c_text"] = "содержит";
            tblOper.Rows.Add(like);

            DataRow nlike = tblOper.NewRow();
            // nlike["c_value"] = "notLike";
            nlike["c_value"] = "not Like";
            nlike["c_text"] = "не содержит";
            tblOper.Rows.Add(nlike);

            DataRow isnull = tblOper.NewRow();
            //isnull["c_value"] = "is null";
            isnull["c_value"] = "is Null";
            isnull["c_text"] = "пустой";
            tblOper.Rows.Add(isnull);

            DataRow isnnull = tblOper.NewRow();
            // isnnull["c_value"] = "is not null";
            isnnull["c_value"] = "is not Null";
            isnnull["c_text"] = "не пустой";
            tblOper.Rows.Add(isnnull);

            DataRow start = tblOper.NewRow();
            start["c_value"] = "start with";
            start["c_text"] = "начинается с";
            tblOper.Rows.Add(start);

            DataRow nstart = tblOper.NewRow();
            nstart["c_value"] = "no start with";
            nstart["c_text"] = "не начинается с";
            tblOper.Rows.Add(nstart);

            DataRow interval = tblOper.NewRow();
            interval["c_value"] = "interval";
            interval["c_text"] = "в интервале (2-5)";
            tblOper.Rows.Add(interval);

            DataRow enumer = tblOper.NewRow();
            enumer["c_value"] = "enumer";
            enumer["c_text"] = "перечисление (2,3,5)";
            tblOper.Rows.Add(enumer);



            ((DataGridViewComboBoxColumn)grdFilter.Columns["c_oper"]).ValueMember = "c_value";
            ((DataGridViewComboBoxColumn)grdFilter.Columns["c_oper"]).DisplayMember = "c_text";
            ((DataGridViewComboBoxColumn)grdFilter.Columns["c_oper"]).DataSource = tblOper;

        }


        public void LoadForm(DataTable _tblFilter, DataGridView Grid)
        {

            tblFilter.Rows.Clear();

            if (_tblFilter.Rows.Count > 0) tblFilter = _tblFilter;


            foreach (DataGridViewColumn col in Grid.Columns)
                {
                    //col.Caption   
                /*
                    if (((DataTable)Grid.DataSource).Columns[col.Name].DataType == typeof(System.Guid) 
                        || ((DataTable)Grid.DataSource).Columns[col.Name].DataType == typeof(System.String) 
                        || ((DataTable)Grid.DataSource).Columns[col.Name].DataType == typeof(System.DateTime))
                        cols.Add(col.Name, 1);
                    else
                        cols.Add(col.Name, 0);
                */

                    tblFields.Rows.Add(col.Name, col.HeaderText);


                }

                ((DataGridViewComboBoxColumn)grdFilter.Columns["c_Columns"]).ValueMember = "c_value";
                ((DataGridViewComboBoxColumn)grdFilter.Columns["c_Columns"]).DisplayMember = "c_text";
                ((DataGridViewComboBoxColumn)grdFilter.Columns["c_Columns"]).DataSource = tblFields;



            grdFilter.DataSource = tblFilter;


        }





        private void grdFilter_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            if (cols.Keys.Contains(((DataGridView)sender).Rows[e.RowIndex].Cells["c_Columns"].Value.ToString()))
                ((DataGridView)sender).Rows[e.RowIndex].Cells["c_type"].Value = cols[((DataGridView)sender).Rows[e.RowIndex].Cells["c_Columns"].Value.ToString()];

            SendKeys.Send("{ENTER}");

        }

        private void frmFilter_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            Appload();

            foreach (DataRow dr in tblFilter.Rows)
            {
                if(dr["c_Columns"].ToString() == "")
                { 
                    MessageBox.Show("Не заполнено поле 'Поле'", "Внимание",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
            }


            ((frmClean)this.Owner).tblFilter = tblFilter;
            this.Close();

        }

        private void Appload()
        {
           // foreach (DataGridViewRow row in grdFilter.Rows)
           // {
            //    if (row.Cells[0].ToString() == "" || row.Cells[1].ToString() == "")
           //         grdFilter.Rows.Remove(row);
          //  }

            grdFilter.EndEdit();

            //((frmClean)this.Owner).tblFilter = tblFilter;
            //((frmClean)this.Owner).GetData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            tblFilter.Rows.Add();

        }

        private void btnDel_Click(object sender, EventArgs e)
        {

            if (grdFilter.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in grdFilter.SelectedRows)
                {
                    grdFilter.Rows.Remove(row);
                }
            }
        }
        private void Grid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFilter_Click(null, null);
        }

    }
}
